package v1

import (
	"github.com/gin-gonic/gin"
	"github.com/Unknwon/com"
	"blog/pkg/errmsg"
	"blog/models"
	"blog/pkg/utils"
	"blog/pkg/settings"
	"net/http"
	"github.com/astaxie/beego/validation"
	"log"
)

//获取多个文章标签
func GetTags(c *gin.Context) {
	name := c.Query("name")
	maps := make(map[string]interface{})
	data := make(map[string]interface{})

	if name != "" {
		maps["name"] = name
	}

	state := -1

	//c.Query可用于获取?name=test&state=1这类URL参数,而c.DefaultQuery则支持设置一个默认值
	if arg := c.Query("state"); arg != "" {
		state, _ = com.StrTo(arg).Int()
		maps["state"] = state
	}

	code := errmsg.SUCCESS
	data["lists"] = models.GetTags(utils.GetPage(c), settings.PageSize, maps)
	data["total"] = models.GetTagTotal(maps)

	c.JSON(http.StatusOK, gin.H{"code": code, "msg": errmsg.GetMsg(code), "data": data})
}

//新增文章标签
func AddTag(c *gin.Context) {
	name := c.Query("name")
	state, _ := com.StrTo(c.DefaultQuery("state", "0")).Int()
	createdBy := c.Query("createdBy")

	valid := validation.Validation{}
	valid.Required(name, "name").Message("名称不能为空")
	valid.Required(name, "createdBy").Message("创建人不能为空")
	valid.MaxSize(name, 100, "createdBy").Message("最长100")
	valid.MaxSize(name, 100, "name").Message("最长100")
	valid.Range(state, 0, 1, "state").Message("状态只允许0或1")

	code := errmsg.INVALID_PARAMS

	if ! valid.HasErrors() {

		if !models.ExistTagByName(name) {
			code = errmsg.SUCCESS
			models.AddTag(name, state, createdBy)
		} else {
			code = errmsg.ERROR_EXIST_TAG
		}

	} else {
		for _, err := range valid.Errors {
			log.Println(err.Key, err.Message)
		}
	}

	c.JSON(http.StatusOK, gin.H{
		"code": code,
		"msg":  errmsg.GetMsg(code),
		"data": make(map[string]string),
	})

}

//修改文章标签

func EditTag(c *gin.Context) {
	id, _ := com.StrTo(c.Param("id")).Int()
	name := c.Query("name")
	modifiedBy := c.Query("modifiedBy")

	vaild := validation.Validation{}

	state := -1

	if arg := c.Query("state"); arg != "" {
		state, _ = com.StrTo(arg).Int()
		vaild.Range(state, 0, 1, "state").Message("状态只允许0或者1")
	}

	vaild.Required(id, "id").Message("id不能为空")
	vaild.Required(modifiedBy, "modifiedBy").Message("修改人不能为空")
	vaild.MaxSize(modifiedBy, 100, "modifiedBy").Message("修改人名字最长100字符")
	vaild.MaxSize(name, 100, "name").Message("名字最长100字符")

	code := errmsg.INVALID_PARAMS

	if ! vaild.HasErrors() {
		code = errmsg.SUCCESS
		if models.ExistTagByID(id) {
			data := make(map[string]interface{})
			data["modifiedBy"] = modifiedBy

			if name != "" {
				name = name
			}
			if state != -1 {
				data["state"] = state
			}
			models.EditTag(id, data)
		} else {
			code = errmsg.ERROR_NOT_EXIST_TAG
		}

	} else {
		for _, err := range vaild.Errors {
			log.Println(err.Key, err.Message)
		}
	}
	c.JSON(http.StatusOK, gin.H{
		"code": code,
		"msg":  errmsg.GetMsg(code),
		"data": make(map[string]string),
	})
}

//删除文章标签
func DeleteTag(c *gin.Context) {
	id, _ := com.StrTo(c.Param("id")).Int()

	valid := validation.Validation{}
	valid.Min(id, 1, "id").Message("Id必须大于0")

	code := errmsg.INVALID_PARAMS

	if !valid.HasErrors() {
		code = errmsg.SUCCESS

		if models.ExistTagByID(id) {
			models.DeleteTag(id)
		} else {
			code = errmsg.ERROR_NOT_EXIST_TAG
		}
	} else {
		for _, err := range valid.Errors {
			log.Println(err.Key, err.Message)
		}
	}
	c.JSON(http.StatusOK, gin.H{
		"code": code,
		"msg":  errmsg.GetMsg(code),
		"data": make(map[string]string),
	})
}
